from __future__ import with_statement
import os.path

import parse

#ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = ""

BASE_DATA_DIR = os.path.join(ROOT_DIR, "data")

file = open(os.path.join(BASE_DATA_DIR, "settings.txt"))

with file:
	for line in file:
		if line.startswith("\t"):
			line = line.strip()
			
			if line:
				if mode == "game":
					type_name, arguments = line.split(":", 1)
					args = parse.parse(arguments, None)[0]
					
					if type_name == "mod":
						MOD = args
					elif type_name == "levelset":
						LEVELSET = args
				elif mode == "editor":
					pass
				else:
					raise Exception("Unknown settings data: %s" % mode)
		elif line.strip() or line.strip().startswith("#"):					
			mode = line.strip().lower()

MOD_DATA_DIR = os.path.join(BASE_DATA_DIR, MOD)
OBJECT_DIR = os.path.join(MOD_DATA_DIR, "objects")

LEVEL_BASE_DIR = os.path.join(ROOT_DIR, "levelsets")
LEVELSET_DIR = os.path.join(LEVEL_BASE_DIR, LEVELSET)

LEVELSET_LEVELS = []
file = open(os.path.join(LEVELSET_DIR, "levelset.txt"))

with file:
	for line in file:
		if line.startswith("\t"):
			line = line.strip()
			
			if line:
				if mode == "levels":
					LEVELSET_LEVELS.append(parse.parse(line, None)[0])
				else:
					raise Exception("Unknown levelset data: %s" % mode)
		elif line.strip() or line.strip().startswith("#"):					
			mode = line.strip().lower()

directions = {
	(0, 0): -1,
	(1, 0): 0,
	(0, 1): 1,
	(-1, 0): 2,
	(0, -1): 3,
	(1, 1): 1,
}

movements = {
	-1: (0, 0),
	0: (1, 0),
	1: (0, 1),
	2: (-1, 0),
	3: (0, -1),
}

def clamp(minimum, value, maximum):
	return max(minimum, min(value, maximum))